package com.kly.sc.api.application.remote.impl;

import com.alibaba.fastjson.JSON;
import com.fqgj.common.api.Response;
import com.kly.sc.api.application.remote.UserSwitchManageRemoteService;
import com.kly.sc.api.application.remote.req.SwitchManageListReq;
import com.kly.user.dto.SwitchManageDto;
import com.kly.user.service.UserSwitchManageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class UserSwitchManageRemoteServiceImpl implements UserSwitchManageRemoteService {
    private static final int SWITCH_ENABLE = 1;
    @Resource
    UserSwitchManageService userSwitchManageService;

    @Override
    public boolean getSwitch(SwitchManageListReq req) {
        Response<List<SwitchManageDto>> response = userSwitchManageService.getSwitchManageList(req.getPackageName(), req.getSwitchType().getCode(), req.getAppVersionName(), req.getAppVersion());
        log.info("getSwitch done. {}, {}", JSON.toJSONString(req), JSON.toJSONString(response));
        if (response == null || !response.isSuccess() || CollectionUtils.isEmpty(response.getData())) {
            return false;
        }
        // 判断开关是否启用
        return response.getData().parallelStream().anyMatch(o -> o.getStatus() == SWITCH_ENABLE);
    }
}
